package com.uwo.commons.dubbo.configuration;

import com.alibaba.dubbo.config.RegistryConfig;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * dubbo配置信息
 * @author hao.yan
 */
@Data
@RefreshScope
@Component("dubboProperties")
@ConfigurationProperties(prefix = "spring.dubbo")
public class DubboProperties {

    private final Logger log = LoggerFactory.getLogger(DubboProperties.class);

    private ApplicationConfigProperties application;

    private ProtocolConfigProperties protocol;

    private RegistryConfigProperties registry;

    private List<RegistryConfigProperties> registries;

    private Map<String, RegistryConfig> registryConfigs;

    /**
     * 多个注入配置
     * @return
     */
    public Map<String, RegistryConfig> getRegistryConfigs(){
        if(null == registryConfigs)
            registryConfigs = new HashMap<String, RegistryConfig>();

        if(null == registries)
            registries = new ArrayList<RegistryConfigProperties>();

        if(registryConfigs.size() == registries.size()){
            return registryConfigs;
        } else {
            registryConfigs.clear();
        }

        // 设置单个配置
        if(null != registry)
            registries.add(registry);

        // 解析配置
        for (DubboProperties.RegistryConfigProperties properties:registries) {
            if(registryConfigs.containsKey(properties.getName())) {
                continue;
            }
            RegistryConfig registryConfig = getRegistryConfig(properties);
            registryConfigs.put(properties.getName(), registryConfig);
        }
        return registryConfigs;
    }

    private RegistryConfig getRegistryConfig(RegistryConfigProperties properties){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(properties.getAddress());
        registryConfig.setId(properties.getName());
        registryConfig.setGroup(properties.getGroup());
        registryConfig.setCheck(properties.getCheck());
        registryConfig.setTimeout(properties.getTimeout());
        return registryConfig;
    }

    /**
     * 应用配置
     */
    @Data
    public static class ApplicationConfigProperties {

        // 应用名称
        private String name;

    }

    /**
     * 协议配置
     */
    @Data
    public static class ProtocolConfigProperties {
        // 服务协议
        private String name = "dubbo";

        // 服务IP地址(多网卡时使用)
        private String host;

        // 服务端口
        private Integer port = 20800;

        // 线程池大小(固定大小)
        private Integer threads = 200;

        // 最大请求数据长度
        private Integer payload = 10 * 1024;

        // 网络传输方式
        private String transporter = "netty";

    }

    /**
     * 注入配置
     */
    @Data
    public static class RegistryConfigProperties {

        private String name = "default";

        private Integer timeout = 60000;

        // 注册中心地址
        private String address;

        private String group = "dubbo";

        // 启动时检查注册中心是否存在
        private Boolean check = false;

    }

}
